package com.yamin.picToCharPaint.utils.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * @description: 图片工具抽象
 * @author：yamin
 * @date: 2021/9/15
 */
public abstract class AbstractImage {

    protected static final String DIC = "@#&$%*o!;.";

    /**
     * 将图片转换成 base64字符串
     *
     * @param image     图片
     * @param precision 转换精度
     * @return base64字符串
     */
    protected String picToBase64(BufferedImage image, int precision, String formatName) throws IOException {

        int minX = image.getMinX();
        int minY = image.getMinY();
        int width = image.getWidth();
        int height = image.getHeight();
        //创建一个新的 BufferedImage 用于绘制新的图片
        BufferedImage newImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //获取绘制上下文
        Graphics graphics = initGraphics(newImg, width, height, precision);

        for (int y = minY; y < height; y += precision) {
            for (int x = minX; x < width; x += precision) {
                //获取坐标像素
                int pixel = image.getRGB(x, y);
                //获取三原色
                int red = (pixel & 0xff0000) >> 16;
                int green = (pixel & 0xff00) >> 8;
                int blue = (pixel & 0xff);
                //转换灰度
                float gray = 0.299f * red + 0.578f * green + 0.114f * blue;
                //将灰度转换成对应字典下标
                int index = Math.round(gray * (DIC.length() + 1) / 255);
                //获取对应字符串
                String c = index >= DIC.length() ? " " : String.valueOf(DIC.charAt(index));
                //绘制像素点
                graphics.drawString(c, x, y);
            }
        }
        graphics.dispose();
        //将 BufferedImage 转换成 字节数据
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(newImg, formatName, out);
        //将带有图片信息的字节数据转换成 base64 字符创
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] encode = encoder.encode(out.toByteArray());
        return new String(encode);
    }

    /**
     * base64 字符串转图片
     *
     * @param base64str base64字符串
     * @return 图片
     * @throws IOException
     */
    protected BufferedImage base64ToPic(String base64str) throws IOException {
        //将带有图片信息的 base64 字符串转换成 BufferedImage
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] decode = decoder.decode(base64str);
        return ImageIO.read(new ByteArrayInputStream(decode));
    }

    private Graphics initGraphics(BufferedImage image, int width, int height, int size) throws IOException {
        if (image == null) {
            throw new IOException("image不能为空");
        }
        Graphics graphics = image.createGraphics();
        //设置背景色
        graphics.setColor(null);
        //绘制背景
        graphics.fillRect(0, 0, width, height);
        //设置前景色
        graphics.setColor(Color.BLACK);
        //设置字体
        graphics.setFont(new Font("微软雅黑", Font.PLAIN, size));
        return graphics;
    }
}
